/**
 *  Copyright (c)  2010  Angel Ortiz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 */

package data;

import data.tileables.Structure;
import data.tileables.Unit;

/**
 * @author(s) Angel Ortiz & Leszek Karcz
 * @date 03/19/2010 This Class servers as a connection between OverViewTables &
 *       GameObjects/DataObjects
 */
public class OverviewData {

	private Player player;
	private String[][] arrayUnits;
	private String[][] arrayStructures;
	private String[][] arrayArmies;
	private int numOfUnits;
	private int numOfStructures;
	private int numOfArmies;
	private int location = 0;
	private int cursorX = 9;
	private int cursorY = 0;
	private int mode = 0;

	public OverviewData(Player player) {

		this.player = player;
	}

	/**
	 * This Method returns a 2D array of the Armies in String format
	 * 
	 * @return
	 */
	public String[][] getArmyData() {
		// *******************************needs to be implemented later
		return this.arrayArmies;
	}

	public String[][] getStructureData() {

		LinkedList<Structure> structures = this.player.getAllStructures();

		// Populate Structures array
		this.numOfStructures = structures.getSize();
		this.arrayStructures = new String[this.numOfStructures - this.location][10];

		for (int i = location; i < this.numOfStructures; i++) {
			this.arrayStructures[i - location][0] = ""
					+ structures.peekIndex(i).getID();
			this.arrayStructures[i - location][1] = ""
					+ structures.peekIndex(i).getType();
			this.arrayStructures[i - location][2] = ""
					+ structures.peekIndex(i).getStats()[3];
			this.arrayStructures[i - location][3] = ""
					+ structures.peekIndex(i).getStats()[4];
			this.arrayStructures[i - location][4] = ""
					+ structures.peekIndex(i).getStats()[2];
			this.arrayStructures[i - location][5] = ""
					+ structures.peekIndex(i).getUpkeep()[0] + " "
					+ structures.peekIndex(i).getUpkeep()[1] + " "
					+ structures.peekIndex(i).getUpkeep()[2];
			this.arrayStructures[i - location][6] = "No Alloc";
			this.arrayStructures[i - location][7] = "PRODUCTION";
			this.arrayStructures[i - location][8] = ""
					+ structures.peekIndex(i).getStats()[0] + " / "
					+ structures.peekIndex(i).getStats()[1];
			this.arrayStructures[i - location][9] = "MISSION ";
		}
		return this.arrayStructures;
	}

	public String[][] getUnitsData() {
		LinkedList<Unit> units = this.player.getAllUnits();
		// Populate Units array
		this.numOfUnits = units.getSize();
		System.out.println("size "+this.numOfUnits);
		this.arrayUnits = new String[this.numOfUnits - this.location][10];

		for (int i = location; i < this.numOfUnits; i++) {
			this.arrayUnits[i - location][0] = "" + units.peekIndex(i).getID();
			this.arrayUnits[i - location][1] = ""
					+ units.peekIndex(i).getType();
			this.arrayUnits[i - location][2] = ""
					+ units.peekIndex(i).getStats()[3];
			this.arrayUnits[i - location][3] = ""
					+ units.peekIndex(i).getStats()[4];
			this.arrayUnits[i - location][4] = ""
					+ units.peekIndex(i).getStats()[2];
			this.arrayUnits[i - location][5] = ""
					+ units.peekIndex(i).getUpkeep()[0] + " "
					+ units.peekIndex(i).getUpkeep()[1] + " "
					+ units.peekIndex(i).getUpkeep()[2];
			this.arrayUnits[i - location][6] = ""
					+ units.peekIndex(i).getMovementTicks();
			this.arrayUnits[i - location][7] = ""
					+ units.peekIndex(i).getStats()[0] + " / "
					+ units.peekIndex(i).getStats()[1];
			this.arrayUnits[i - location][8] = "Host Owner ";
			this.arrayUnits[i - location][9] = "MISSION ";
		}
		return this.arrayUnits;
	}

	public int getNumOfArmies() {
		return this.numOfArmies;
	}

	public int getNumOfStructures() {
		return this.numOfStructures;
	}

	public int getNumOfUnits() {
		return this.numOfUnits;
	}

	public void nextMode() {
		this.mode++;
		if (this.mode > 2)
			this.mode = 0;
		location = 0;
		this.cursorY=0;
	}

	public void previousMode() {
		this.mode--;
		if (this.mode < 0)
			this.mode = 2;
		location = 0;
		this.cursorY=0;
	}

	public void moveCursorDown() {
		System.out.println(this.arrayUnits.length);
		this.cursorY++;
		if (this.cursorY > 8) {
			this.cursorY = 8;

			if (mode == 0) {
				if (this.arrayUnits.length> 9) {
					this.location++;

				}
			}
			else if(mode == 1) {
				if (this.arrayStructures.length> 9) {
					this.location++;
				}
			}
			else if(mode == 2) {
				if (this.arrayArmies.length> 9) {
					this.location++;
				}
			}
		}
		
		if (mode == 0)
		{
			if(this.cursorY > this.arrayUnits.length-1)
			{
				this.cursorY=this.arrayUnits.length-1;
			}
		}
		else if(mode == 1)
		{
			if(this.cursorY > this.arrayStructures.length-1)
			{
				this.cursorY=this.arrayStructures.length-1;
			}
		}
		else if(mode == 1)
		{
			if(this.cursorY > this.arrayArmies.length-1)
			{
				this.cursorY=this.arrayArmies.length-1;
			}
		}
		
	}

	public void moveCursorUp() {
		this.cursorY--;
		if (this.cursorY < 0) {
			this.cursorY = 0;
			if (this.location > 0) {
				this.location--;
				if (this.location < 0) {
					this.location = 0;
				}
			}
		}
	}

	public void moveCursorRight() {
		this.cursorX++;
		if (this.cursorX > 9) {
			this.cursorX = 9;
		}
	}

	public void moveCursorLeft() {
		this.cursorX--;
		if (this.cursorX < 8) {
			this.cursorX = 8;
		}
	}

	public int[] getCursorPosition() {
		return new int[] { this.cursorX, this.cursorY };
	}
	public boolean needsArrowUp(){
		if(this.location > 0)
			return true;
		else
			return false;
	}
	public boolean needsArrowDown(){
		if(this.mode == 0 && this.getUnitsData().length > 9)
			return true;
		else if(this.mode == 1 && this.getStructureData().length > 9)
			return true;
		else if (this.mode == 2 && this.getArmyData().length > 9)
			return true;
		else
			return false;
	}

}
